package it.valeandre.omino;

import it.valeandre.omino.adapter.MatchDetailsSlidePagerAdapter;
import it.valeandre.omino.fragment.UpgradableBaseFragment;
import it.valeandre.omino.model.Item;
import it.valeandre.omino.model.Matches;
import it.valeandre.omino.model.RSS;
import it.valeandre.omino.model.Ranking;
import it.valeandre.omino.model.Results;
import it.valeandre.omino.model.Setting;
import it.valeandre.omino.model.TeamsList;
import it.valeandre.omino.utils.MultiDirectionSlidingDrawer;
import it.valeandre.omino.utils.ResBundle;

import java.util.Observer;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;



public class MatchDetailsActivity extends BaseFragmentActivity {

    private Handler mHandler = new Handler();
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    private LinearLayout mArrowPanel;
    private ImageView mLeftArrow;
    private ImageView mRightArrow;
    private boolean isArrowPanelAnimating = false;
    private int mCurrentPosition = 0;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        setContentView(R.layout.activity_match_details);
        Bundle data = getIntent().getBundleExtra(ResBundle.KEY_INTENT_MATCH_DETAIL_DATA_BUNDLE);
        //Match match  = (Match)data.getSerializable(ResBundle.KEY_BUNDLE_MATCH_DETAIL_DATA);

        mPager = (ViewPager) findViewById(R.id.pager_activity_match_details);

        mArrowPanel = (LinearLayout) findViewById(R.id.activity_match_details_arrow_panel);
        mLeftArrow = (ImageView) findViewById(R.id.activity_match_details_arrow_left);
        mLeftArrow.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (mCurrentPosition > 0) {
					mPager.setCurrentItem(mCurrentPosition - 1);
				}
				
			}
        });
        mRightArrow = (ImageView) findViewById(R.id.activity_match_details_arrow_right);
        mRightArrow.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (mCurrentPosition <  MatchDetailsSlidePagerAdapter.NUM_PAGES - 1) {
                    mPager.setCurrentItem(mCurrentPosition + 1);
                }
            }
        });

        mPagerAdapter = new MatchDetailsSlidePagerAdapter(getSupportFragmentManager(), this, data);
        mPager.setAdapter(mPagerAdapter);
        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {
                super.onPageSelected(pos);
                mCurrentPosition = pos;
                if (pos == MatchDetailsSlidePagerAdapter.NUM_PAGES - 1) {
                    mLeftArrow.setVisibility(View.VISIBLE);
                    mRightArrow.setVisibility(View.GONE);
                } else if (pos == 0) {
                    mLeftArrow.setVisibility(View.GONE);
                    mRightArrow.setVisibility(View.VISIBLE);
                } else {
                    mLeftArrow.setVisibility(View.VISIBLE);
                    mRightArrow.setVisibility(View.VISIBLE);
                }
            }
        });
        mPager.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                showArrowPanelForAWhile();
                return false;
            }
        });

        showArrowPanelForAWhile();

    }

    @Override
    protected void onResume() {
        initBanners();
        super.onResume();
    }

    public void showArrowPanelForAWhile() {
        if (!isArrowPanelAnimating) {
            isArrowPanelAnimating = true;
            fadeIn(mArrowPanel);
            mHandler.postDelayed(new Runnable() {
                public void run() {
                    fadeOut(mArrowPanel);
                }
            }, 3000);
        }

    }

    private void fadeIn(final LinearLayout panel) {
        panel.setVisibility(View.VISIBLE);
        Animation fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setInterpolator(new DecelerateInterpolator());
        fadeIn.setDuration(600);
        panel.startAnimation(fadeIn);
    }

    private void fadeOut(final LinearLayout panel) {
        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
        fadeOut.setDuration(600);
        fadeOut.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {

            }

            public void onAnimationEnd(Animation animation) {
                isArrowPanelAnimating = false;
                panel.setVisibility(View.GONE);
            }

            public void onAnimationRepeat(Animation animation) {

            }
        });
        panel.startAnimation(fadeOut);
    }

    public void register() {

    }

    public void expandActionBar() {

    }

    public void hideActionBar() {

    }

    public void showProgress() {

    }

    public void hideProgress() {

    }

    public void loadTeams(boolean manageStartup, Observer observer) {

    }

    public void loadResults(boolean manageStartup, Observer observer) {

    }

    public void loadTmpMatches(boolean manageStartup, Observer observer) {

    }

    public void loadRanking(boolean manageStartup, Observer observer) {

    }

    public void loadSettings(boolean manageStartup, Observer observer) {

    }

    public String getSettingTeamsString() {
        return null;
    }

    public void setSettingPushFlag(boolean flag) {

    }

    public boolean getSettingPushFlag() {
        return false;
    }

    public void setTeams(TeamsList mTeams) {

    }

    public void switchToFeedsFragment(String url) {

    }

    public void switchToFeedDetailFragment(Item feed) {

    }

    public Ranking getRanking() {
        return null;
    }

    public Results getResults() {
        return null;
    }

    public Matches getMatches() {
        return null;
    }

    public Matches getTmpResults() {
        return null;
    }

    public TeamsList getTeams() {
        return null;
    }

    public void updateTeamFeeds(String url, RSS feeds) {

    }

    public RSS getTeamFeeds(String url) {
        return null;
    }

    public void showDialog(String message) {

    }

    public void showDialog(String message, OnClickListener positiveListener) {

    }

    public MultiDirectionSlidingDrawer getSlider() {
        return null;
    }

    public void performBack() {

    }

    public boolean isGCMRegistrationError() {
        return false;
    }

    public ImageButton getRefreshImageButton() {
        return null;
    }

    public void setCurrentFragment(UpgradableBaseFragment fragment) {

    }

    public Setting getSettings() {
        return null;
    }

    @Override
    protected void refreshCurrentPage() {

    }

	public void showDialog(String message, android.content.DialogInterface.OnClickListener positiveListener) {
		
	}
}
